<?php
class relax extends MX_Controller
{
    private $limit;
    public function __construct()
    {
        parent::__construct();
        $this->limit = 10;
        $this->load->helper('text');
        $this->load->helper('image');
    }
    
    public function index()
    {
        $data = array(
            'title' => 'Thư giãn',
            'meta_des' => 'Thư giãn',
            'meta_key' => 'Thư giãn',
            );
        $this->load->view("templates/master", $data);
    }
    public function listArt($cid, $offset = 0)
    {
        $limit = 10;
        $this->load->model('Relax_Articles_Model');
        $artModel = new Relax_Articles_Model();
        $list = $artModel->getArticleByCatetory($cid, $offset, $limit);
        $ctocal = $artModel->CountArticleByCatetory($cid);
        $paging = paging_fo('relax/listArtAjax/'.$cid, $ctocal, $limit, 4);
        return $this->load->view('categorys/list', array(
            'list' => $list,
            'paging' => $paging
            ), TRUE);
    }
    public function listArtAjax($cid, $offset = 0)
    {
        $limit = 1;
        $this->load->model('Relax_Articles_Model');
        $artModel = new Relax_Articles_Model();
        $list = $artModel->getArticleByCatetory($cid, $offset, $limit);
        $ctocal = $artModel->CountArticleByCatetory($cid);
        $paging = paging_fo('relax/listArtAjax/'.$cid, $ctocal, $limit, 4);
        $this->load->view('categorys/list_ajax', array(
            'list' => $list,
            'paging' => $paging
            ));
    }
    public function catetory()
    {
        $alias = trim($this->uri->segment(1));
        $this->load->model('Relax_Categories_Model');
        $cateModel = new Relax_Categories_Model();
        $cid = $cateModel->getCatetoryByAlias($alias);

        if ($cid == 0) {
            return redirect(base_url());
        }
        $this->load->model('Relax_Articles_Model');
        $artModel = new Relax_Articles_Model();
        $top1 = $artModel->top1($cid);
        
        $hit = $artModel->getTopHitArticleByCatetory($cid);
        $content = $this->load->view('categorys/top', array('top1' => $top1), TRUE);
        $content1 = $this->load->view('categorys/hit', array('hit' => $hit), TRUE);
        $list = $this->listArt($cid, 0);
        
        $newCom = $artModel->newCommentBycatetory($cid);
        $newcomment = $this->load->view('categorys/binhluanmoi', array('newCom' => $newCom), TRUE);
        
        $this->load->view("templates/master_relax_category", array(
            'content' => $content,
            'content1' => $content1,
            'list' => $list,
            'newcomment' => $newcomment,
            'title' => 'Thư giãn',
            'meta_des' => 'Thư giãn',
            'meta_key' => 'Thư giãn',
            ));
    }
    
    
    
    public function ajax_comment($id = 0, $offset = 0)
    {
        $id = (int)$id;
        $offset = (int)$offset;
        $this->load->model('Relax_Articles_Comments_Model');
        $commentModel = new Relax_Articles_Comments_Model();
        $comment = $commentModel->getCommentByAid($id, $offset);
        $this->load->view('detail/comment_ajax', array('comment' => $comment)); 
    }
    public function detail($alias = '')
    {
        $this->load->model('Relax_Articles_Model');
        $artModel = new Relax_Articles_Model();
        $id = $artModel->getIdByAlias($alias);
        if ($id == 0) {
            return redirect(base_url());
        }
        $artModel->updateHit($id);
        $result = $artModel->getArticleById($id);
        $content = $this->load->view('detail/detail', array('result' => $result), TRUE);
        
        $this->load->model('Relax_Articles_Comments_Model');
        $commentModel = new Relax_Articles_Comments_Model();
        $comment = $commentModel->getCommentByAid($id);
        $ctocal = $commentModel->ctotal($id);
        $paging = paging_fo('relax/ajax_comment/'.$id, $ctocal, 2, 4);
        
        $content1 = $this->load->view('detail/comment', array(
            'comment' => $comment, 
            'paging' => $paging), TRUE);
        $hit = $artModel->articleByCatetory($result->cid);    
        $xemnhieu = $this->load->view('detail/xemnhieu', array('hit' => $hit), TRUE);    
        $this->load->view("templates/master_detail", array(
            'content' => $content,
            'aid' => $id,
            'xemnhieu' => $xemnhieu,
            'content1' => $content1
        ));
    }
    
    
    public function popup_display()
    {
        $this->load->model('util/captcha_model');
        $captcha = new Captcha_Model();
        $imgCaptcha = $captcha->captcha_generate();
        $this->load->view('detail/captcha', array('image' =>  $imgCaptcha));
    }
    
    
    public function captchasubmit()
    {
        $this->load->model('util/captcha_model');
        $captcha = new Captcha_Model();
        $arr = array();
        if (!$captcha->captcha_check()) {
            $imgCaptcha = $captcha->captcha_generate();
            $arr['success'] = 0;
            $arr['captcha'] = $imgCaptcha;
            echo json_encode($arr);
            exit();
        } else {
            $this->load->model('Relax_Articles_Comments_Model');
            $commentModel = new Relax_Articles_Comments_Model();
            $commentModel->addComment();
            $arr['success'] = 1;
            echo json_encode($arr);
            exit();
        }        
    }
    
}